package view;
import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * This Class is a Login window which extends JFrame Object.
 * @author Dmitry
 *
 */
public class LoginGUI extends AbstractGUI{
	
	// Deceleration of Login components
	/** The label of user id. */
	private JLabel lblUserID = null;
	
	/** The label of password. */
	private JLabel lblPassword = null;
	
	/** The text field of user id. */
	private JTextField txtUserID = null;
	
	/** The text field of password. */
	private JPasswordField pswPassword = null;
	
	/** The warning message. */
	private JLabel wrnMessage = null;
	
	/** The register button. */
	private JButton btnRegister = null;
	
	/** The login button. */
	private JButton btnLogin = null;
	
	/**
	 * This is Login window Constructor.
	 */
	public LoginGUI() {
		getContentPane().setLayout(null);
		getContentPane().add(getLblUserID());
		getContentPane().add(getLblPassword());
		getContentPane().add(getTxtUserID());
		getContentPane().add(getPswPassword());
		getContentPane().add(getWrnMessage());
		getContentPane().add(getBtnLogin());
		getContentPane().add(getBtnRegister());
		
		this.setTitle("Login");
		this.setBounds(500, 200, 312, 178);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		
		}

	/**
	 * This method creates UserId label if currently is null.
	 * @return JLabel type.
	 */
	private JLabel getLblUserID() {
		if(lblUserID == null){
			lblUserID = new JLabel("UserID");
			lblUserID.setBounds(10, 11, 59, 14);
		}
		return lblUserID;
	}
	
	/**
	 * This method creates Password label if currently is null.
	 * @return JLabel type.
	 */
	private JLabel getLblPassword() {
		if(lblPassword == null){
			lblPassword = new JLabel("Password");
			lblPassword.setBounds(10, 36, 59, 14);
		}
		return lblPassword;
	}
	
	/**
	 * This method creates UserId text field if currently is null.
	 * @return JTextField type.
	 */
	private JTextField getTxtUserID() {
		if(txtUserID == null){
			txtUserID = new JTextField();
			txtUserID.setBounds(79, 8, 189, 20);
			txtUserID.setColumns(10);
		}
		return txtUserID;
	}
	
	/**
	 * This method creates Password text field if currently is null.
	 * @return JPAsswordField type.
	 */
	private JPasswordField getPswPassword() {
		if(pswPassword == null){
			pswPassword = new JPasswordField();
			pswPassword.setBounds(79, 33, 189, 20);
		}
		return pswPassword;
	}
	
	/**
	 * This method creates warning message if currently is null.
	 * @return JLabel type.
	 */
	private JLabel getWrnMessage() {
		if (wrnMessage == null) {
			wrnMessage = new JLabel("You MUST fill all the the fields!");
			wrnMessage.setForeground(Color.RED);
			wrnMessage.setBounds(10, 72, 198, 14);
			wrnMessage.setVisible(false);
		}
		return wrnMessage;
	}
	
	/**
	 * This method creates Register button if currently is null.
	 * @return JButton type.
	 */
	private JButton getBtnRegister() {
		if(btnRegister == null){
			btnRegister = new JButton("Register");
			btnRegister.setBounds(60, 116, 89, 23);
			//TO-ADD register controller to open new register window
		}
		return btnRegister;
	}
	
	/**
	 * This method creates Login button if currently is null.
	 * @return JButton type.
	 */
	private JButton getBtnLogin() {
		if(btnLogin == null){
			btnLogin = new JButton("Login");
			btnLogin.setBounds(159, 116, 89, 23);
		}
		return btnLogin;
	}
	
	/**
	 * This method adds action listener to register button.
	 *
	 * @param listener the listener
	 */
	public void addRegisterActionListener(ActionListener listener){
		btnRegister.addActionListener(listener);
	}
	
	/**
	 * This method add action listener to login button.
	 *
	 * @param listener the listener
	 */
	public void addLoginActionListener(ActionListener listener){
		btnLogin.addActionListener(listener);
	}
	
	/**
	 * Gets the user id filed.
	 *
	 * @return the user id filed
	 */
	public String getUserIDFiled(){
		return txtUserID.getText();
	}
	
	/**
	 * Clear fields.
	 */
	public void clearFields() {
		txtUserID.setText("");
		pswPassword.setText("");
	}
	
	/**
	 * Display warning message.
	 *
	 * @param msg the warning message
	 */
	public void displayWrnMessage(String msg){
		wrnMessage.setText(msg);
		wrnMessage.setVisible(true);
	}
	
	/**
	 * Undisplay warning message.
	 */
	public void undisplayWrnMessage(){
		wrnMessage.setVisible(false);
	}
	
	/**
	 * Gets the password field.
	 *
	 * @return the password field
	 */
	public String getPasswordField(){
		char [] pass = pswPassword.getPassword();
		String password = new String(pass);
		return password;
	}
	
}
